package thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class ThreadPoolTest {

    public static class MyTask implements Runnable {
        @Override
        public void run() {
            System.out.println("MyTask:Thread ID:"
                    + Thread.currentThread().getId());

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        ExecutorService executorService = Executors.newSingleThreadExecutor();

        for (int i = 0; i < 10; i++) {
            executorService.submit(new MyTask());
        }


        Thread.sleep(1500);
        executorService.submit(new MyTask());

        Thread.sleep(1500);
        executorService.submit(new MyTask());
        executorService.submit(new MyTask());
        executorService.submit(new MyTask());

        Runnable thrownExceptionTask = () -> {
            System.out.println("thrownExceptionTask run");
            throw new RuntimeException("thrownExceptionTask demo");
        };

        try {
            executorService.submit(thrownExceptionTask).get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
//        executorService.execute(thrownExceptionTask);

        executorService.shutdown();
    }
}
